package itheima_edu_info_manager.com.itheima.edu.info.manager.controller;

import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Student;
import itheima_edu_info_manager.com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    Scanner sc = new Scanner(System.in);
    StudentService studentService = new StudentService();
    //该方法是开启学生管理系统，并进行展示！
    public void start() {
        //对循环进行命名： studentloop:
        while (true){
            //学生管理系统菜单的搭建
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice){
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    upDateStudentById();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理！");
                    return;
                    //通过对循环进行命名，然后使用break studentloop;结束此次的循环，但是并不影响InfoManagerEntry中的循环
                default:
                    System.out.println("您输入有误，请重新输入！");
                    break;
            }
        }
    }
    //添加学生
    public void addStudent() {
        String id;
        while (true){
            System.out.println("请输入学生的id：");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag){
                System.out.println("学号已被占用，请重新输入！");
            }else {
                break;
            }
        }
        Student student = getStudent(id);
        boolean result = studentService.addStudent(student);
        if (result){
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }

    }
    //删除学生
    public void deleteStudentById() {
        while (true){
            String id = putStudentId();
            studentService.deleteStudentById(id);
            System.out.println("删除成功");
            break;
        }
    }
    //修改学生
    public void upDateStudentById() {
        String id = putStudentId();
        Student student = getStudent(id);
        studentService.upDateStudentById(id,student);
        System.out.println("修改成功！");

    }
    //查询学生
    public void findAllStudent() {
        Student[] stu = studentService.findAllStudent();
        if (stu == null) {
            System.out.println("查无信息，请添加后再尝试！");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < stu.length; i++) {
                Student stus = stu[i];
                if (stus != null){
                    System.out.println(stus.getId() + "\t" + stus.getName() + "\t" +stus.getAge() + "\t\t" + stus.getBirthday());
                }
            }
        }
    //将接收到的信息进行封装
    public Student getStudent(String id) {
        System.out.println("请输入你修改后的姓名:");
        String name = sc.next();
        System.out.println("请输入你修改后的年龄:");
        String age = sc.next();
        System.out.println("请输入你修改后的生日:");
        String birthday = sc.next();

        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        student.setBirthday(birthday);
        return student;
    }
    //输入id的判断是否存在
    public String putStudentId() {
        String id;
        while (true){
            System.out.println("请输入学生id:");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (!flag){
                System.out.println("你输入的学生id有误，请重新输入！");
            }
            studentService.deleteStudentById(id);
            System.out.println("成功");
            break;
        }
        return id;
    }

}
